/*
 * Production.java
 *
 * Created on July 14, 2006, 3:05 PM
 *
 */

package galronnlp.util;

import galronnlp.cfg.grammar.Rule;
import java.util.Iterator;

/**
 *
 * @author Daniel A. Galron
 */
public class Production implements Comparable {

    private Rule value;
    private int rep;    
    
    /** Creates a new instance of Production */
    public Production(Rule v, int r) {
        this.rep = r;
        this.value = v;
    }
    
    public Rule getValue() {
        return this.value;
    }
    
    public String getXMLRep() {
        String ret = "";
        if(this.value.hasTerminalRHS()) {
            ret += "<lex cat=\"" + this.value.LHS() + "\" entry = \"" + this.value.RHS().get(0) + "\"/>\n";
        } else {
            ret += "<rule lhs=\"" + this.value.LHS() + "\">\n";
            for(Iterator<Symbol> it = this.value.RHS().iterator(); it.hasNext(); ) {
                ret += "\t<child node=\"" + it.next()  + "\"/>\n";
            }
            ret += "</rule>\n";
        }
        return ret;
    }
    
    public int compareTo(Object o) {
        Production other = (Production)o;
        if(this.rep > other.rep)
            return 1;
        else if(this.rep < other.rep)
            return -1;
        else
            return 0;
    }
    
    public boolean equals(Object o) {
        Production other = (Production)o;
        return this.rep == other.rep;
    }
    
    public String toString() {
        return this.value.toString();
    }
    
    public int hashCode() {
        return this.rep;
    }    
    
}
